package com.coolgor.axnut.web.airuc;

import com.coolgor.axnut.dto.auth.LocalUserExecution;
import com.coolgor.axnut.entity.auth.LocalUser;
import com.coolgor.axnut.enums.auth.LocalUserOperationStateEnum;
import com.coolgor.axnut.enums.auth.LocalUserStateEnum;
import com.coolgor.axnut.service.auth.LocalUserService;
import com.coolgor.axnut.util.HttpServletRequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
@RequestMapping(value="airuc", method={RequestMethod.GET,RequestMethod.POST})
public class AirucUserController {
    @Autowired
    private LocalUserService localUserService;

    @RequestMapping(value = "/getUserList", method={RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    private Map<String, Object> getLocalUserList(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        int page = 0 ;
        int pageSize = 0;
        try {
            page = Integer.parseInt(request.getParameter("page"));
            pageSize = Integer.parseInt(request.getParameter("rows"));
        } catch (Exception e) {
            modelMap.put("success", false);
            modelMap.put("errMsg", e.toString());
        }
        LocalUser localUserCondition = new LocalUser();
        localUserCondition.setUsername(HttpServletRequestUtil.getString(request,"username"));
        localUserCondition.setNameCN(HttpServletRequestUtil.getString(request,"name_cn"));
        localUserCondition.setStatus(HttpServletRequestUtil.getInteger(request,"status"));
        LocalUserExecution exe = localUserService.getLocalUserList(localUserCondition, page, pageSize);
        if (null != exe | exe.getState()== LocalUserOperationStateEnum.SUCCESS.getState()) {
            try {
                modelMap.put("total",exe.getCount());
                modelMap.put("rows",exe.getLocalUserList());
                modelMap.put("success", true);
            } catch (Exception e) {
                modelMap.put("success", false);
                modelMap.put("errMsg", e.toString());
            }
        } else {
            modelMap.put("success", false);
            modelMap.put("errMsg", "Empty List");
        }
        return modelMap;
    }

    @RequestMapping(value = "/changeUsername", method={RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    private Map<String, Object> changeUsername(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        LocalUser localUser = new LocalUser();
        localUser.setUserId(HttpServletRequestUtil.getLong(request,"user_id"));
        if (localUser.getUserID()==-1L) {
            modelMap.put("success", false);
            modelMap.put("errMsg", "UserID未能传递到后台！");
            return modelMap;
        }
        localUser.setUsername(HttpServletRequestUtil.getString(request,"username"));
        LocalUserExecution exe = localUserService.modifyLocalUser(localUser);
        if (null != exe ) {
            if (exe.getState() == LocalUserOperationStateEnum.SUCCESS.getState()) {
                modelMap.put("success", true);
            } else if (exe.getState() == LocalUserOperationStateEnum.EXISTUSERNAME.getState()){
                modelMap.put("success", false);
                modelMap.put("errMsg", "操作失败：用户名已存在");
            } else if (exe.getState() == LocalUserOperationStateEnum.NULL_USER.getState()){
                modelMap.put("success", false);
                modelMap.put("errMsg", "操作失败：用户ID不存在");
            } else {
                modelMap.put("success", false);
                modelMap.put("errMsg", "未知错误，操作失败！");
            }
        } else {
            modelMap.put("success", false);
            modelMap.put("errMsg", "未知错误，操作失败！");
        }
        return modelMap;
    }

    @RequestMapping(value = "/changeUserStatus", method={RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    private Map<String, Object> changeUserStatus(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        LocalUser localUser = new LocalUser();
        localUser.setUserId(HttpServletRequestUtil.getLong(request,"user_id"));
        if (localUser.getUserID()==-1L) {
            modelMap.put("success", false);
            modelMap.put("errMsg", "UserID未能传递到后台！");
            return modelMap;
        }
        localUser.setStatus(HttpServletRequestUtil.getInteger(request,"status"));
        LocalUserExecution exe = localUserService.modifyLocalUser(localUser);
        if (null != exe && exe.getState()== LocalUserOperationStateEnum.SUCCESS.getState()) {
            try {
                modelMap.put("success", true);
            } catch (Exception e) {
                modelMap.put("success", false);
                modelMap.put("errMsg", "操作失败："+e.toString());
            }
        } else {
            modelMap.put("success", false);
            modelMap.put("errMsg", "未知错误，操作失败！");
        }
        return modelMap;
    }

    @RequestMapping(value = "/getUserStateEnum", method={RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    private Map<String, Object> getUserStateEnum(HttpServletRequest request) {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        modelMap.put("success", true);
        modelMap.put("data", LocalUserStateEnum.toJsonString());
        return modelMap;
    }


}
